package com.persondemo.videoappliction.ui.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.VideoBean;

import java.util.List;

/**
 * ExpandableListView 适配器
 */
public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    /**
     * 每个分组的名字的集合
     */
    private List<VideoBean.DataBean.LaiyuanBean> groupList;
    /**
     * 所有分组的所有子项的 GridView 数据集合
     */
    private List<List<VideoBean.DataBean.LaiyuanBean.JishuBean>> itemList;

    private GridView gridView;
    private Handler handler;

    public MyExpandableListViewAdapter(Context context, List<VideoBean.DataBean.LaiyuanBean> groupList,
                                       List<List<VideoBean.DataBean.LaiyuanBean.JishuBean>> itemList) {
        mContext = context;
        this.groupList = groupList;
        this.itemList = itemList;
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                notifyDataSetChanged();
                super.handleMessage(msg);
            }
        };
    }

    public void refresh() {
        handler.sendMessage(new Message());
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return itemList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup
            parent) {
        if (null == convertView) {
            convertView = View.inflate(mContext, R.layout.expandablelist_group, null);
        }
        ImageView ivGroup = (ImageView) convertView.findViewById(R.id.iv_group);
        TextView tvGroup = (TextView) convertView.findViewById(R.id.tv_group);
        RelativeLayout grop_rl = convertView.findViewById(R.id.grop_rl);
        // 如果是展开状态，就显示展开的箭头，否则，显示折叠的箭头
        if (isExpanded) {
            ivGroup.setImageResource(R.drawable.up);
        } else {
            ivGroup.setImageResource(R.drawable.down);
        }
        // 设置分组组名
        if (groupList.size() != 0) {
            tvGroup.setText(groupList.get(groupPosition).getName());
        }
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = View.inflate(mContext, R.layout.expandablelist_item, null);
        }
        // 因为 convertView 的布局就是一个 GridView，
        // 所以可以向下转型为 GridView
        gridView = (GridView) convertView;
        // 创建 GridView 适配器
        if (groupList.size() != 0) {
            MyGridViewAdapter gridViewAdapter = new MyGridViewAdapter(mContext, groupList.get(groupPosition).getJishu());
            gridView.setAdapter(gridViewAdapter);
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
